<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Symfony\Component\HttpFoundation\Response;

class RedirectAuthenticated
{
    /**
     * Handle an incoming request.
     *@param \Illuminate\Contracts\Http\Request $request
     * @param  \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response)  $next
     *@param string|null ...$guards
     * @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
     */
    public function handle(Request $request, Closure $next,...$guards)
    {
        $guards = empty($guards)?[null]:$guards;

        foreach ($guards as $guard) {
            if(Auth::guard($guard)->check()){
                session()->flash('warning','您已登录，无需操作该项！');
                return redirect(RouteServiceProvider::HOME);
            }
        }

        return $next($request);
    }
}
